package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.*;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R selectAll(Integer page, Integer pageSize, String name) {

        Page<Setmeal> page1 = new Page<>(page, pageSize);

        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name", name);
        }
        setmealService.page(page1, wrapper);
        List<Setmeal> records = page1.getRecords();
        for (Setmeal record : records) {
            //根据id查找数据
            Category byId = categoryService.getById(record.getCategoryId());
            //获取name赋值给dish属性
            record.setCategoryName(byId.getName());
        }
        return R.success(page1);
    }

    /**
     * 添加
     *
     * @param set
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R Add(@RequestBody DishSet set) {

        setmealService.Add(set);

        return R.success("添加成功");

    }

    /**
     * 停售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R updateByIdT(Long[] ids) {

        Setmeal setmeal = new Setmeal();
        for (Long id : ids) {
            setmeal.setId(id);
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    /**
     * 起售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R updateByIdQ(Long[] ids) {
        Setmeal setmeal = new Setmeal();
        for (Long id : ids) {
            setmeal.setId(id);
            setmeal.setStatus(1);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    /**
     * 数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {

        Setmeal byId = setmealService.getById(id);

        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);

        List<SetmealDish> list = setmealDishService.list(wrapper);

        DishSet dishSet = new DishSet();
        dishSet.setSetmealDishes(list);
        //数据拷贝
        BeanUtils.copyProperties(byId, dishSet);

        return R.success(dishSet);

    }

    /**
     * 修改
     * @param set
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R update(@RequestBody DishSet set) {

        setmealService.updateBI(set);

        return R.success("修改成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R delete(@RequestParam List<Long> ids) {

        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        wrapper.eq("status", 1);
        int count = setmealService.count(wrapper);
        if (count > 0) {
            return R.error("该套餐正在起售,无法删除");
        }
        setmealService.removeByIds(ids);

        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        //删除中间表数据
        queryWrapper.in("setmeal_id", ids);
        setmealDishService.remove(queryWrapper);

        return R.success("删除成功");
    }

    /**数据回显
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R dish(@PathVariable Long id) {

        Setmeal byId = setmealService.getById(id);

        return R.success(byId);
    }

    @GetMapping("/list")
    @Cacheable(value = "setmeal",key = "#p0")
    public R list(Long categoryId, int status) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        wrapper.eq("status", status);
        List<Setmeal> list = setmealService.list(wrapper);

      /*  List<DishSet> dishSets = new ArrayList<>();
        for (Setmeal setmeal : list) {

            QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("setmeal_id", setmeal.getId());
            List<SetmealDish> list1 = setmealDishService.list(queryWrapper);
            DishSet dishSet = new DishSet();
            dishSet.setSetmealDishes(list1);
            BeanUtils.copyProperties(list, dishSet);
            dishSets.add(dishSet);

        }*/

        return R.success(list);
    }
}
